package com.example.a517;

import android.Manifest;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class NotificationActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_send;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_notification);

        btn_send = findViewById(R.id.send_notification);


        btn_send.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.POST_NOTIFICATIONS},1);
        }else {
            Toast.makeText(this,"已经有权限",Toast.LENGTH_SHORT).show();
        }

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification =  new NotificationCompat.Builder(this,"ll")
                .setContentTitle("这是内容标题")
                .setContentText("这是内容")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.aa)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.bb))
                .setAutoCancel(true) // 设置通知自动消失
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.aa)))
                .build();
//        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
//        {
//            NotificationChannel channel = new NotificationChannel("ll","ll",NotificationManager.IMPORTANCE_HIGH);
//            manager.createNotificationChannel(channel);
//        }
        manager.notify(1,notification);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED ){
                        Toast.makeText(this,"权限已经开启",Toast.LENGTH_SHORT).show();
                    }
        }
    }
}